package com.open.capacity.reimburse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.commons.Result;
import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.reimburse.dao.SysNoticeDao;
import com.open.capacity.reimburse.dao.SysNoticeReadDao;
import com.open.capacity.reimburse.entity.SysNotice;
import com.open.capacity.reimburse.entity.SysNoticeRead;
import com.open.capacity.reimburse.service.SysNoticeService;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.SysUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class SysNoticeServiceImpl implements SysNoticeService {

    @Autowired
    private SysNoticeDao noticeDao;
    @Autowired
    private SysNoticeReadDao readDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysNotice notice) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();
        notice.setCreateTime(new Date());
        notice.setCreateUserid(loginUser.getId());
        notice.setCreateUsername(loginUser.getRealName());
        noticeDao.save(notice);
        log.info("保存公告：{}", notice);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SysNotice notice) {
        noticeDao.updateByOps(notice);
        log.info("修改公告：{}", notice);
    }

    @Override
    public void publish(String id) {
        SysNotice notice = noticeDao.findById(id);
        notice.setStatus(2);
        noticeDao.updateByOps(notice);
        log.info("发布公告：{}", notice);
    }

    @Override
    public void read(String id) {
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();
        SysNoticeRead exists = readDao.findByNoticeIdAndUserid(id, loginUser.getId());
        if(exists==null) {
            SysNoticeRead read = new SysNoticeRead();
            read.setNoticeId(id);
            read.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            read.setReadTime(new Date());
            read.setReadUserid(loginUser.getId());
            read.setReadUsername(loginUser.getRealName());
            readDao.save(read);
            log.info("阅读公告：{}", read);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        SysNotice notice = noticeDao.findById(id);
        noticeDao.delete(id);
        log.info("删除公告：{}", notice);
    }


    @Override
    public SysNotice findById(String id) {
        read(id);
        return noticeDao.findById(id);
    }

    @Override
    public PageResult<SysNoticeRead> pageReads(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        String noticeId = params.get("id").toString();
        List<SysNoticeRead> list = readDao.findByNoticeId(noticeId);
        PageInfo<SysNoticeRead> pageInfo = new PageInfo(list);

        return PageResult.<SysNoticeRead>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    @Override
    public List<SysNotice> findTop5(Map<String, Object> params) {
        return noticeDao.findTop5(params);
    }

    @Override
    public PageResult<SysNotice> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<SysNotice> list = noticeDao.findList(params);
        PageInfo<SysNotice> pageInfo = new PageInfo(list);

        return PageResult.<SysNotice>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    @Override
    public Result saveOrUpdate(SysNotice notice, String keyValue) {
        int i = 0;
        if (StringUtils.isNotEmpty(keyValue)) {
            SysNotice model = noticeDao.findById(keyValue);
            if (model != null) {
                notice.setId(keyValue);
                i = noticeDao.updateByOps(notice);
            }
        } else {
            LoginAppUser loginUser = SysUserUtil.getLoginAppUser();
            notice.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            notice.setCreateTime(new Date());
            notice.setCreateUserid(loginUser.getId());
            notice.setPv(0);
            notice.setCreateUsername(loginUser.getRealName());
            i = noticeDao.save(notice);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }
}
